﻿using PaddleOCRSharp;
using System.Drawing;
using Tesseract;
using PaddleOCRSharp;
namespace TlToolCordCreate
{
    public class OCRUtils : IDisposable
    {
        PaddleOCREngine engine;

        //中英文模型V3模型
        OCRModelConfig config = null;

        public OCRUtils()
        {
            ini();
        }
        public void ini()
        {
            //OCR参数
            OCRParameter oCRParameter = new OCRParameter();
            oCRParameter.cpu_math_library_num_threads = 6;//预测并发线程数
            oCRParameter.enable_mkldnn = true;//是否使用mkldnn模型
            oCRParameter.cls = false; //是否执行文字方向分类
            oCRParameter.use_angle_cls = false;//是否开启方向检测
            oCRParameter.det_db_score_mode = true;//是否使用多段线，即文字区域是用多段线还是用矩形，
            oCRParameter.det_db_unclip_ratio = 1.6f;
            oCRParameter.max_side_len = 2000;
            //初始化OCR引擎
            engine = new PaddleOCREngine(config, oCRParameter);
        }
        public string BitmapToString(Bitmap bitmap)
        {
           // var imagebyte = File.ReadAllBytes("1.png");
            OCRResult ocrResult = engine.DetectText(bitmap);
            //Console.WriteLine(ocrResult.Text);
            return ocrResult.Text;
        }

        private TesseractEngine _engineForchi = null;
        private TesseractEngine _engineForeng = null;


        public string test_string(Bitmap bitmap)
        {
            if (_engineForchi == null)
            {
                _engineForchi = new TesseractEngine("tessdata-main", "chi_sim", EngineMode.Default);
            }
            using (var page = _engineForchi.Process(bitmap))
            {
                // 获取识别结果
                return page.GetText();
            }
        }
        public string test_int(Bitmap bitmap)
        {
            if (_engineForeng == null)
            {
                _engineForeng = new TesseractEngine("tessdata-main", "eng", EngineMode.Default);
            }
            using (var page = _engineForeng.Process(bitmap))
            {
                return page.GetText();
            }
        }

        public void Dispose()
        {
            if (_engineForchi != null) { _engineForchi.Dispose(); }
            if (_engineForeng != null) { _engineForeng.Dispose(); }
        }
    }
}